package com.nwpu.intimacy.adapter;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.nwpu.intimacy.R;
import com.nwpu.intimacy.chat.ListData;

@SuppressLint({ "SdCardPath", "InflateParams" })
public class MessageAdapter extends BaseAdapter {

	private List<ListData> lists;
	private Context mContext;
	private RelativeLayout layout;

	private MediaPlayer mPlayer = null; // 用于语音播放

	public MessageAdapter(List<ListData> lists, Context mContext) {

		this.lists = lists;
		this.mContext = mContext;
		mPlayer = new MediaPlayer();
	}

	@Override
	public int getCount() {
		return lists.size();
	}

	@Override
	public Object getItem(int position) {
		return lists.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		if (lists.get(position).getType() == 1) {

			LayoutInflater inflater = LayoutInflater.from(mContext);
			if (lists.get(position).getFlag() == ListData.RECEIVER) {
				layout = (RelativeLayout) inflater.inflate(
						R.layout.leftitem_text, null);
			}
			if (lists.get(position).getFlag() == ListData.SEND) {
				layout = (RelativeLayout) inflater.inflate(
						R.layout.rightitem_text, null);
			}
			final TextView tv = (TextView) layout.findViewById(R.id.tv_chat);
			TextView time = (TextView) layout.findViewById(R.id.time_chat);
			ImageView photo = (ImageView) layout.findViewById(R.id.iv_chat);
			TextView name = (TextView) layout.findViewById(R.id.username_chat);

			tv.setText(lists.get(position).getContent());
			time.setText(lists.get(position).getTime());

			if (lists.get(position).getFlag() == ListData.RECEIVER) {

				name.setText(lists.get(position).getFriendname());

				@SuppressWarnings("deprecation")
				Drawable drawable = new BitmapDrawable(lists.get(position)
						.getFriendPhoto());
				photo.setImageDrawable(drawable);

			} else {

				name.setText(lists.get(position).getUsername());

				@SuppressWarnings("deprecation")
				Drawable drawable = new BitmapDrawable(lists.get(position)
						.getUserPhoto());
				photo.setImageDrawable(drawable);

			}

			tv.setOnLongClickListener(new OnLongClickListener() {

				@Override
				public boolean onLongClick(View v) {

					ClipboardManager cmb = (ClipboardManager) mContext
							.getSystemService(Context.CLIPBOARD_SERVICE);
					cmb.setText(tv.getText().toString());

					Toast.makeText(mContext, "Copy Success!",
							Toast.LENGTH_SHORT).show();

					return false;
				}
			});

		} else if (lists.get(position).getType() == 2) {

			LayoutInflater inflater = LayoutInflater.from(mContext);
			if (lists.get(position).getFlag() == ListData.RECEIVER) {
				layout = (RelativeLayout) inflater.inflate(
						R.layout.leftitem_photo, null);
			}
			if (lists.get(position).getFlag() == ListData.SEND) {
				layout = (RelativeLayout) inflater.inflate(
						R.layout.rightitem_photo, null);
			}

			ImageView tv = (ImageView) layout.findViewById(R.id.tv_chat);
			TextView time = (TextView) layout.findViewById(R.id.time_chat);
			ImageView photo = (ImageView) layout.findViewById(R.id.iv_chat);
			TextView name = (TextView) layout.findViewById(R.id.username_chat);

			try {

				FileInputStream fis = new FileInputStream(lists.get(position)
						.getContent());
				Bitmap bitmap_tv = BitmapFactory.decodeStream(fis);

				Drawable drawable_tv = new BitmapDrawable(bitmap_tv);
				tv.setImageDrawable(drawable_tv);

			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			time.setText(lists.get(position).getTime());

			if (lists.get(position).getFlag() == ListData.RECEIVER) {

				name.setText(lists.get(position).getFriendname());

				@SuppressWarnings("deprecation")
				Drawable drawable = new BitmapDrawable(lists.get(position)
						.getFriendPhoto());
				photo.setImageDrawable(drawable);

			} else {

				name.setText(lists.get(position).getUsername());

				@SuppressWarnings("deprecation")
				Drawable drawable = new BitmapDrawable(lists.get(position)
						.getUserPhoto());
				photo.setImageDrawable(drawable);

			}

		} else if (lists.get(position).getType() == 3) {

			LayoutInflater inflater = LayoutInflater.from(mContext);
			if (lists.get(position).getFlag() == ListData.RECEIVER) {
				layout = (RelativeLayout) inflater.inflate(
						R.layout.leftitem_voice, null);
			}
			if (lists.get(position).getFlag() == ListData.SEND) {
				layout = (RelativeLayout) inflater.inflate(
						R.layout.rightitem_voice, null);
			}

			ImageView tv = (ImageView) layout.findViewById(R.id.tv_chat);
			TextView time = (TextView) layout.findViewById(R.id.time_chat);
			ImageView photo = (ImageView) layout.findViewById(R.id.iv_chat);
			TextView name = (TextView) layout.findViewById(R.id.username_chat);

			time.setText(lists.get(position).getTime());

			if (lists.get(position).getFlag() == ListData.RECEIVER) {

				name.setText(lists.get(position).getFriendname());

				@SuppressWarnings("deprecation")
				Drawable drawable = new BitmapDrawable(lists.get(position)
						.getFriendPhoto());
				photo.setImageDrawable(drawable);

			} else {

				name.setText(lists.get(position).getUsername());

				@SuppressWarnings("deprecation")
				Drawable drawable = new BitmapDrawable(lists.get(position)
						.getUserPhoto());
				photo.setImageDrawable(drawable);

			}

			tv.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

					try {

						mPlayer.reset();
						mPlayer.setDataSource(lists.get(position).getContent());
						mPlayer.prepare();
						mPlayer.start();

					} catch (IllegalArgumentException e) {
						e.printStackTrace();
					} catch (SecurityException e) {
						e.printStackTrace();
					} catch (IllegalStateException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					}

				}
			});

		}

		return layout;
	}

	/**
	 * 将字符串转换成Bitmap图片
	 * 
	 * @param string
	 * @return
	 */
	public Bitmap stringtoBitmap(String string) {
		// 将字符串转换成Bitmap类型
		Bitmap bitmap = null;
		try {
			byte[] bitmapArray;
			bitmapArray = Base64.decode(string, Base64.DEFAULT);
			bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0,
					bitmapArray.length);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return bitmap;
	}
}